package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.constants.RedisConstant;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IUserService userService;


    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        Long userId = UserHolder.getUser().getId();

//        判断关注还是取关
        if(isFollow){
//        关注，继续新增数据
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);
            boolean isSaved = save(follow);
            if(isSaved){
                String key = RedisConstant.FOLLOWS + userId;
                stringRedisTemplate.opsForZSet().add(key, followUserId.toString(), System.currentTimeMillis());
            }
        } else {
//      取关就删除
            boolean isRemoved = remove(new QueryWrapper<Follow>().eq("user_id", userId)
                    .eq("follow_user_id", followUserId));

            if(isRemoved){
                String key = RedisConstant.FOLLOWS + userId;
                stringRedisTemplate.opsForZSet().remove(key, followUserId.toString());
            }
        }

        return Result.ok();
    }

    @Override
    public Result isFollow(Long followUserId) {
        Long userId = UserHolder.getUser().getId();
//        查询当前用户是否关注了

        if(query().eq("user_id", userId).eq("follow_user_id", followUserId).count() > 0){
            return Result.ok(true);
        }
        return Result.ok(false);
    }



    @Override
    public Result followCommons(Long id) {
//        获取当前用户
        Long userId = UserHolder.getUser().getId();
//        获取和用户之间的交集
        String key = RedisConstant.FOLLOWS + userId;

        String key2 = RedisConstant.FOLLOWS + id;

        Set<String> intersect = stringRedisTemplate.opsForZSet().intersect(key, key2);

        if(intersect == null || intersect.isEmpty()){
            return Result.ok(Collections.emptyList());
        }

        List<Long> ids = intersect.stream().map(Long::valueOf).toList();

        List<UserDTO> usersDTO = userService.listByIds(ids).stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .toList();

        return Result.ok(usersDTO);

    }
}
